package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentCateGoryService;
import com.dongdongshop.service.ContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @Reference
    private ContentService contentService;

    @Reference
    private ContentCateGoryService contentCateGoryService;

    @RequestMapping("show")
    public String show(){
        return "admin/content";
    }

    @RequestMapping("contentList")
    @ResponseBody
    public Result contentList(){
        try {
            List<TbContent> content = contentService.contentList();
            return Result.response(ResultEnum.SUCCESS).setData(content);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //分类查询
    @RequestMapping("findCategory")
    @ResponseBody
    public Result findCategory(){
        try {
            TbContentCategory tbContentCategory = new TbContentCategory();
            tbContentCategory.setName("");
            List<TbContentCategory> content = contentCateGoryService.contentCatGoryList(tbContentCategory.getName());
            return Result.response(ResultEnum.SUCCESS).setData(content);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(TbContent tbContent){
        try {
           Boolean content = contentService.save(tbContent);
            return Result.response(ResultEnum.SUCCESS).setData(content);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result getById(@RequestParam("id") long id){
        try {
            TbContent content = contentService.getById(id);
            return Result.response(ResultEnum.SUCCESS).setData(content);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("contentUpdate")
    @ResponseBody
    public Result contentUpdate(TbContent tbContent){
        try {
            Integer content = contentService.contentUpdate(tbContent);
            return Result.response(ResultEnum.SUCCESS).setData(content);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("deleteContent")
    @ResponseBody
    public Result deleteContent(@RequestParam("id[]")Long[] id){
        try {
            Integer i = contentService.deleteContent(id);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
}
